import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import {router} from '@/router'
import {Message} from 'element-ui'
import { getToken, removeToken } from '@/utils/auth'
import qs from 'qs'
// 必须实例化 Vue
let vm = new Vue()
axios.defaults.timeout = 10000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
// http request 拦截器
axios.interceptors.request.use(
    config => {
        if (process.env.NODE_ENV === 'production') {
            if (config.url.indexOf('login') > -1 || config.url === '/admin/user/modify/password') {
                config.baseURL = vm.LOGIN_URL
            } else if (config.url.indexOf('pim') > -1) {
                config.baseURL = vm.DSR_URL
            } else {
                config.baseURL = vm.BASE_URL
            }
        } else {
            if (config.url.indexOf('login') > -1 || config.url === '/admin/user/modify/password') {
                console.log(process.env)
                config.baseURL = process.env.LOGIN_URL
            } else if (config.url.indexOf('pim') > -1) {
                config.baseURL = process.env.DSR_URL
            } else {
                config.baseURL = process.env.BASE_URL
            }
        }
        // Do something before request is sent
        if (store.getters.token) {
            // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
            config.headers['ticket'] = getToken()
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// http response 拦截器
axios.interceptors.response.use(
    response => {
        return response
    },
    error => {
        if (error.response.status === 500) {
            Message({showClose: true,
                message: error.response.data.msg,
                type: 'error',
                duration: 2000})
        } else {
            removeToken()
            router.push({
                path: '/login'
            })
        }
        return Promise.reject(error)
    }
)

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function fetch (url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(data), {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}
/**
 * 封装post请求(contentType为application/json)
 * @param url
 * @param data
 * @returns {Promise}
 */
export function postWithJson (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                'Content-type': 'application/json'
            }
        })
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}
/**
 * 文件上传
 * @param url
 * @param  data
 */
export function upload (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}
/**
 * formdata文件上传
 * @param url
 * @param  data
 */
export function uploadFormdata (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                'processData': false,
                'contentType': false
            }
        })
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}
/**
 * 文件下载
 * @param url
 * @param  data
 */
export function download (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, data, {
            'responseType': 'arraybuffer'
        }).then(response => {
            if (response.status === 200) {
                resolve(response.data)
            }
        }, err => {
            reject(err)
        })
    })
}
/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function petch (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                } else {
                    reject(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
    })
}
/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function del (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, data)
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                } else {
                    reject(response)
                }
            }, err => {
                reject(err)
            })
    })
}
/**
 * 封装delWithJson请求(contentType为application/json)
 * @param url
 * @param data
 * @returns {Promise}
 */
export function delWithJson (url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {data: data}, {
            headers: {
                'Content-type': 'application/json'
            }
        })
            .then(response => {
                if (response.status === 200) {
                    resolve(response.data)
                }
            }, err => {
                reject(err)
            })
    })
}
